package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"post-pub/handler"
)

func Router() *gin.Engine {
	r := gin.Default()
	r.Use(Cors()) //默认跨域
	r.POST("/user/login", handler.Login)
	r.POST("/roleNode/roleNodeAdd", handler.RoleNodeAdd)  //角色与权限关联
	r.GET("/userRole/userRoleList", handler.UserRoleList) //用户角色的查询功能
	r.GET("/roleNode/roleNodeList", handler.RoleNodeList) //角色权限信息查询

	r.POST("/depot/depotAdd", handler.DepotAdd)                    //仓库信息添加
	r.POST("/order/depotOrderAdd", handler.DepotOrderAdd)          //入库订单添加
	r.GET("/orderList/depotOrderList", handler.DepotOrderList)     //订单列表
	r.GET("/orderList/depotOrderDrtail", handler.DepotOrderDetail) //订单详细信息的展示
	return r
}
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
